﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace IcarusProject
{
    public class FPSCounter : DrawableGameComponent
    {
        #region Attributs
        private SpriteBatch _spriteBatch;
        private SpriteFont _font;
        private Vector2 _position;

        private int _fps; //Nombre de FPS calcules
        private int _nbFrame;//Nombre d'update en cours
        private float _timeElapsed;//Temps ecoule

        #endregion

        #region Constructeur
        public FPSCounter(Game game) : base(game)
        {
        }
        #endregion

        #region Proprietes
        #endregion

        #region Methodes
        public override void Initialize()
        {
            base.Initialize();

            _spriteBatch = new SpriteBatch(this.GraphicsDevice);
            _position = new Vector2(5);
            _fps = 0;
            _nbFrame = 0;
            _timeElapsed = 0;
        }

        protected override void LoadContent()
        {
            _font = Game.Content.Load<SpriteFont>("FPS_font");
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            _nbFrame++;
            _timeElapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (_timeElapsed >= 1000)
            {
                _fps = _nbFrame;
                _nbFrame = 0;
                _timeElapsed -= 1000;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            _spriteBatch.Begin();

            _spriteBatch.DrawString(_font, _fps.ToString(), _position, Color.Yellow);

            _spriteBatch.End();
        }
        #endregion
    }
}
